﻿using System;
using System.Threading.Tasks;
using Prism.Mvvm;
using Prism.Commands;
using System.Windows;
using WpfRedis.Common;
using WpfRedis.Models;
using Prism.Ioc;
using Unity;
using WpfRedis.Views;
using Prism.Events;
using Prism.Regions;
using MS.Common;
using System.Collections.Generic;

namespace WpfRedis.ViewModels
{
    public class LoginViewModel : VmBase
    {

        public LoginViewModel(IContainerExtension container, IEventAggregator eventAggregator, IRegionManager regionManager) : base(container, eventAggregator, regionManager)
        {
            CloseCommand = new DelegateCommand<object>(OnCloseCommand);
            LoginCommand = new DelegateCommand<object>(OnLoginCommand);
            this.EnterKeyEventCommand = new DelegateCommand<object>(OnEnterKeyCommand);
            LoginData = new LoginModel();
            //可录入下拉框
            //https://www.bilibili.com/video/BV1AV4y1579x/?p=25&spm_id_from=pageDriver
        }

        #region 属性
        private LoginModel _LoginData;
        /// <summary>
        /// 登录信息实体
        /// </summary>
        public LoginModel LoginData
        {
            get
            {
                return _LoginData;
            }
            set
            {
                _LoginData = value;
                RaisePropertyChanged(nameof(LoginData));
            }
        }
        private bool _Enable = true;

        public bool Enable
        {
            get { return _Enable; }
            set
            {
                _Enable = value;
                RaisePropertyChanged(nameof(Enable));
            }
        }


        #endregion

        #region 时间

        /// <summary>
        /// 关闭登录窗体
        /// </summary>
        public DelegateCommand<object> CloseCommand { get; private set; }
        public DelegateCommand<object> LoginCommand { get; private set; }
        public DelegateCommand<object> EnterKeyEventCommand { get; private set; }
        #endregion

        /// <summary>
        /// 关闭程序
        /// </summary>
        /// <param name="obj"></param>
        public void OnCloseCommand(object obj)
        {
            if (obj is Window)
            {
                (obj as Window).Close();
                Environment.Exit(0);
            }
        }

        private Window Win = null;
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="obj"></param>
        public void OnLoginCommand(object obj)
        {
            if (obj is Window win)
            {
                Win = win;
            }
            var a = this.LoginData;
            //if (string.IsNullOrEmpty(LoginData.LoginName) || string.IsNullOrEmpty(LoginData.Password))
            //{
            //    LoginData.ErrorMsg = "用户名或密码不能为空";
            //    return;
            //}
            //else
            //{
            //    LoginData.ErrorMsg = "";
            //}
            Task.Run(() =>
            {
                Enable = false;
                Task.Delay(200).Wait();
                Enable = true;
                GlobalValues.CurrUser = this.LoginData;
                try
                {
                    if (Win != null)
                    {
                        //子线程中调用UI线程
                        Application.Current.Dispatcher.Invoke(new Action(() =>
                        {
                            Win.Close();
                            var main = container.Resolve<AppMainView>();
                            RegionManager.SetRegionManager(main, regionManager);
                            //RegionManager.UpdateRegions();
                            Application.Current.MainWindow = main;
                            Application.Current.MainWindow.Show();
                        }));
                    }
                }
                catch (Exception ex)
                {
                }
            });
        }

        /// <summary>
        /// 回车触发方法   2022-01-20
        /// </summary>
        public void OnEnterKeyCommand(object obj)
        {
            OnLoginCommand(obj);
        }
    }
}
